package com.test.prolenders.dialog_prolenders;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.test.prolenders.R;
import com.test.prolenders.adapter_prolenders.AccountChannelViewAdapter_prolenders;
import com.test.prolenders.bean_prolenders.config_prolenders.AccountChannel_Fr_prolenders;
import com.test.prolenders.bean_prolenders.config_prolenders.AppShowCustomize_prolenders;
import com.test.prolenders.config_prolenders.CountryConfig_prolenders;
import com.test.prolenders.listener_prolenders.BindAccountListener_Fr_prolenders;
import com.test.prolenders.util_prolenders.http_prolenders.HttpUtil_prolenders;


public class BindAccountDialog_Fr_prolenders extends Dialog {

    private FragmentActivity activity;

    private final String page;

    private final BindAccountListener_Fr_prolenders bindAccountListener;

    private final AccountChannelViewAdapter_prolenders accountChannelViewAdapter;

    public BindAccountDialog_Fr_prolenders(@NonNull FragmentActivity activity, String page, BindAccountListener_Fr_prolenders bindAccountListener) {
        super(activity);
        this.activity = activity;
        this.page = page;
        this.bindAccountListener = bindAccountListener;

        accountChannelViewAdapter = new AccountChannelViewAdapter_prolenders(AppShowCustomize_prolenders.getInstance().getAccountChannels(),page);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bindaccount_fr_prolenders);

        getWindow().setBackgroundDrawableResource(R.color.transparent_prolenders);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        findViewById(R.id.dbf_ib_close).setOnClickListener(v -> {
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"DIALOG_CLOSE_CLICK","");
            dismiss();
        });

        //初始化渠道选择器
        RecyclerView channelRv = findViewById(R.id.dbf_recyclerview);
        channelRv.setAdapter(accountChannelViewAdapter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);//网格布局管理器
        gridLayoutManager.setOrientation(RecyclerView.VERTICAL);
        channelRv.setLayoutManager(gridLayoutManager);

        //绑卡输入
        TextView areacode1Tv = findViewById(R.id.dbf_tv_areacode1);
        TextView areacode2Tv = findViewById(R.id.dbf_tv_areacode2);
        EditText mobile1Et = findViewById(R.id.dbf_et_mobile1);
        EditText mobile2Et = findViewById(R.id.dbf_et_mobile2);

        mobile1Et.setOnFocusChangeListener((v, hasFocus) ->
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"DIALOG_PHONE_INPUT",hasFocus ? "":mobile1Et.getText().toString().trim())
        );

        mobile2Et.setOnFocusChangeListener((v, hasFocus) ->
            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"DIALOG_PHONE_CONFIRM_INPUT",hasFocus ? "":mobile2Et.getText().toString().trim())
        );

        areacode1Tv.setText(CountryConfig_prolenders.getInstance().getAreaCode());
        areacode2Tv.setText(CountryConfig_prolenders.getInstance().getAreaCode());

        //绑卡提交
        findViewById(R.id.dbf_btn_add).setOnClickListener(v -> {

            HttpUtil_prolenders.getInstance().saveBuriedPoint(page,"DIALOG_SAVE_CLICK","");

            String mobile1 = mobile1Et.getText().toString().trim();
            String mobile2 = mobile2Et.getText().toString().trim();

            if (TextUtils.isEmpty(mobile1)) mobile1 = "";
            if (TextUtils.isEmpty(mobile2)) mobile2 = "";

            AccountChannel_Fr_prolenders selectChannel = accountChannelViewAdapter.getSelectChannel();
            String channel = selectChannel==null ? "":selectChannel.getValue();

            String finalMobile = mobile1;

            HttpUtil_prolenders.getInstance().addAccount_Fr(
                    activity,
                    mobile1,
                    mobile2,
                    channel,
                    () -> {if (bindAccountListener !=null) bindAccountListener.onBindAccount(finalMobile);},
                    false
            );

            dismiss();

        });

    }

}
